package com.example.schoolnewsapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private LinearLayout  home_1;
    private LinearLayout  home_2;
    private LinearLayout  home_4;
    private MainFragment mainFragment;
    private List<Fragment> fragmentList = new ArrayList<>();
    private MineFragment mineFragment;
    private VideoFragment  videoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "70dab80128229e866f6e73455aa811ab");
        setContentView(R.layout.activity_main);
        home_1 = (LinearLayout)findViewById(R.id.home_1);
        home_2 = (LinearLayout)findViewById(R.id.home_2);
        home_4 = (LinearLayout)findViewById(R.id.home_4);



        initFragment();


        home_1.setOnClickListener(this);
        home_2.setOnClickListener(this);
        home_4.setOnClickListener(this);

    }

    void initFragment() {
        mainFragment = new MainFragment();
        addFragment(mainFragment);
        showFragment(mainFragment);


    }




    private void addFragment(Fragment fragment) {
        //获取fragment管理器,打开事务,把内容显示至布局,提交
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.add(R.id.home_content, fragment).commit();
            fragmentList.add(fragment);
        }
    }

    //显示fragment
    private void showFragment(Fragment fragment) {
        for (Fragment frag : fragmentList) {
            if (frag != fragment) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //隐藏布局
                transaction.hide(frag).commit();
            }
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.show(fragment).commit();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_1: {
                if (mainFragment == null) {
                    mainFragment = new MainFragment();
                }
                addFragment(mainFragment);
                showFragment(mainFragment);
            }
            break;
            case R.id.home_2: {
                if (videoFragment == null) {
                    videoFragment = new VideoFragment();
                }
                addFragment(videoFragment);
                showFragment(videoFragment);


            }
            break;
            case R.id.home_4: {
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                }
                addFragment(mineFragment);
                showFragment(mineFragment);
            }
            break;
            default:
                break;
        }
    }

}